describe('App._apiAuthoriseTransaction', function() {
    it('getAuthoriseTransactions', function() {
        var callback = function() {};
        spyOn(App.api_authoriseTransaction, '_processAuthoriseTransactions');
        spyOn(bsoi, 'authoriseTransactionList').andCallFake(function(req, cb){
            cb({});
        });
        App.api_authoriseTransaction.getAuthoriseTransactions(callback);
        expect(bsoi.authoriseTransactionList).toHaveBeenCalledWith({
            requestHeader : App.getSession().getBreezeContext()
        }, jasmine.any(Function));
        expect(App.api_authoriseTransaction._processAuthoriseTransactions).toHaveBeenCalledWith({});
    });

    describe('_processAuthoriseTransactions', function() {
        it('error response', function() {
            App.api_authoriseTransaction.callbackAuthoriseTransactions = function() {};
            spyOn(App.api_authoriseTransaction, 'callbackAuthoriseTransactions');
            spyOn(bsoi, 'validateResponse').andThrow(BreezeException.create());

            App.api_authoriseTransaction._processAuthoriseTransactions({});

            expect(App.api_authoriseTransaction.callbackAuthoriseTransactions).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        });
        it('transactionList is missing in response', function() {
            App.api_authoriseTransaction.callbackAuthoriseTransactions = function() {};
            spyOn(App.api_authoriseTransaction, 'callbackAuthoriseTransactions');
            spyOn(bsoi, 'validateResponse');

            App.api_authoriseTransaction._processAuthoriseTransactions({
                'authorizeResponse': {
                }
            });

            expect(App.api_authoriseTransaction.callbackAuthoriseTransactions).toHaveBeenCalledWith(null);
        });
        it('transactionList is empty', function() {
            App.api_authoriseTransaction.callbackAuthoriseTransactions = function() {};
            spyOn(App.api_authoriseTransaction, 'callbackAuthoriseTransactions');
            spyOn(bsoi, 'validateResponse');

            App.api_authoriseTransaction._processAuthoriseTransactions({
                'authorizeResponse': {
                    'transactionList': []
                }
            });

            expect(App.api_authoriseTransaction.callbackAuthoriseTransactions).toHaveBeenCalledWith(null);
        });
        it('transactionList has data', function() {
            App.api_authoriseTransaction.callbackAuthoriseTransactions = function() {};
            spyOn(App.api_authoriseTransaction, 'callbackAuthoriseTransactions');
            spyOn(bsoi, 'validateResponse');
            spyOn(App.api_authoriseTransaction, '_buildTransactionList').andReturn([[],[]]);

            App.api_authoriseTransaction._processAuthoriseTransactions({
                'authorizeResponse': {
                    'transactionList': [{

                    }, {

                    }]
                }
            });

            expect(App.api_authoriseTransaction.callbackAuthoriseTransactions).toHaveBeenCalledWith([[],[]], null);
        });
    });
    it('_buildTransactionList', function() {
        var transactions = [
            {
                transferDate: '2013-05-16T10:29:16.663+0800'
            },
            {
                transferDate: '2013-05-16T10:29:16.663+0800'
            },
            {
                transferDate: '2013-05-17T10:29:16.663+0800'
            }
        ];

        App.api_authoriseTransaction._buildTransactionList(transactions);
    });

    it('confirmTransaction', function() {
        var transferData = {
        }
        var transferObj = Ember.Object.create({
            otpToken: '123456',
            transferData: function() {
                return transferData;
            }
        });
        var callback = function() {

        };
        spyOn(bsoi, 'authoriseTransaction').andCallFake(function(request, callback){
            callback({});
        });

        spyOn(transferObj, 'get').andCallThrough();

        spyOn(App.api_authoriseTransaction, 'processConfirmTransactionResponse');
        spyOn(App.getSession(), 'getCurrentTransfer').andReturn(transferObj);

        App.api_authoriseTransaction.confirmTransaction(transferObj, callback);

        expect(bsoi.authoriseTransaction).toHaveBeenCalled();
        expect(transferObj.get).toHaveBeenCalledWith('otpToken');
        expect(App.getSession().getBreezeContext().serviceContext.otpToken).toBe(null);
        expect(App.api_authoriseTransaction.processConfirmTransactionResponse).toHaveBeenCalled();

    });

    describe('processConfirmTransactionResponse', function() {
        it('error response', function() {
            spyOn(bsoi, 'validateResponse').andThrow(BreezeException.create());
            var objectCallback = {
                callback : function() {

                }
            }
            spyOn(objectCallback, 'callback');
            App.api_authoriseTransaction.processConfirmTransactionResponse({}, objectCallback.callback, {});

            expect(objectCallback.callback).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        });
        it('success response', function() {
            spyOn(bsoi, 'validateResponse');
            var objectCallback = {
                callback : function() {

                }
            }
            var response = {
                authorizeTransferResponse: {
                    transactionResult: {
                        transactionStatus: 'SUCC',
                        transactionId: '1234'
                    }
                }
            }
            spyOn(objectCallback, 'callback');
            App.api_authoriseTransaction.processConfirmTransactionResponse({}, objectCallback.callback, response);

            expect(objectCallback.callback).toHaveBeenCalledWith(App.TransferResult.create(response.authorizeTransferResponse.transactionResult));
        });
    });

    it('declineTransaction', function() {
        var transferData = {
        }
        var transferObj = Ember.Object.create({
            transferData: function() {
                return transferData;
            }
        });
        var callback = function() {

        };
        spyOn(bsoi, 'declineTransaction').andCallFake(function(request, callback){
            callback(transferObj, callback, {});
        });

        spyOn(App.api_authoriseTransaction, 'processDeclineTransactionResponse');

        App.api_authoriseTransaction.declineTransaction(transferObj, callback);

        expect(bsoi.declineTransaction).toHaveBeenCalledWith({
            transactionId : transferObj.transferData(),
            requestHeader : App.getSession().getBreezeContext()
        }, jasmine.any(Function));
        expect(App.api_authoriseTransaction.processDeclineTransactionResponse).toHaveBeenCalled();
    });

    describe('processDeclineTransactionResponse', function() {
        it('error response', function() {
            spyOn(bsoi, 'validateResponse').andThrow(BreezeException.create());
            var objectCallback = {
                callback : function() {

                }
            }
            spyOn(objectCallback, 'callback');
            App.api_authoriseTransaction.processDeclineTransactionResponse({}, objectCallback.callback, {});

            expect(objectCallback.callback).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        });
        it('success response', function() {
            spyOn(bsoi, 'validateResponse');
            var objectCallback = {
                callback : function() {

                }
            }
            var response = {
                authorizeTransferResponse: {
                }
            }
            spyOn(objectCallback, 'callback');
            App.api_authoriseTransaction.processDeclineTransactionResponse({}, objectCallback.callback, response);

            expect(objectCallback.callback).toHaveBeenCalledWith(null);
        });
    });

    it('regenerateETAC', function() {
        var transferData = {
        }
        var transferObj = Ember.Object.create({
            transferData: function() {
                return transferData;
            }
        });
        var callback = function() {

        };
        spyOn(bsoi, 'regenerateETAC').andCallFake(function(request, callback){
            callback(transferObj, callback, {});
        });

        spyOn(App.api_authoriseTransaction, 'processRegenerateETACResponse');

        App.api_authoriseTransaction.regenerateETAC(transferObj, callback);

        expect(bsoi.regenerateETAC).toHaveBeenCalledWith({
            transactionId : transferObj.transferData(),
            requestHeader : App.getSession().getBreezeContext()
        }, jasmine.any(Function));
        expect(App.api_authoriseTransaction.processRegenerateETACResponse).toHaveBeenCalled();
    });

    describe('processRegenerateETACResponse', function() {
        it('error response', function() {
            spyOn(bsoi, 'validateResponse').andThrow(BreezeException.create());
            var objectCallback = {
                callback : function() {

                }
            }
            spyOn(objectCallback, 'callback');
            App.api_authoriseTransaction.processRegenerateETACResponse({}, objectCallback.callback, {});

            expect(objectCallback.callback).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        });
        it('success response', function() {
            spyOn(bsoi, 'validateResponse');
            var objectCallback = {
                callback : function() {

                }
            }
            var response = {
                etacResponse: {
                }
            }
            spyOn(objectCallback, 'callback');
            App.api_authoriseTransaction.processRegenerateETACResponse({}, objectCallback.callback, response);

            expect(objectCallback.callback).toHaveBeenCalledWith(response.etacResponse);
        });
    });

})